<?php

namespace app\http\middleware;

use app\util\ReturnCode;
use think\facade\Cache;

class CheckSms {

    /**
     * 短信检测
     * @param \think\facade\Request $request
     * @param \Closure $next
     * @return mixed|\think\response\Json
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function handle($request, \Closure $next) {
        $header = config('apiadmin.CROSS_DOMAIN');
        $ip = $request->ip();
        $smsTime = Cache::get('sms_' . $ip);
        if($smsTime){
            $data = ['code' => ReturnCode::SEND_SMS_FREQUENT_ERROR, 'msg' => '发送短信频率过高，请1分钟后再试', 'data' => []];
            return json($data)->header($header);
        }
        return $next($request);
    }
}
